import {Component, OnInit} from '@angular/core';
import {NzUploadFile} from 'ng-zorro-antd/upload';
import {NzMessageService} from 'ng-zorro-antd/message';
import {PermissionService} from '../../../service/permission.service';
import {AisiPriceService} from '../aisi-price.service';

@Component({
  selector: 'app-quote-manage',
  templateUrl: './quote-manage.component.html',
  styleUrls: ['./quote-manage.component.scss']
})
export class QuoteManageComponent implements OnInit {
  query = {
    page: 1,
    pageSize: 20,
  };
  listOfData = {
    total: 0,
    records: []
  };
  addQuoteModal = {
    visible: false,
    loading: false,
    id: '',
    nameExport: '', // 报价单名称
    nameShow: '', // 报价单名称
    brandId: '', // 品牌id
    brandName: '', // 品牌名称
    type: '', // 类型
    sort: '', // 排序
    imgUrl: '', // 图标url
    status: 1, // 状态
  };
  brandOptions: any[] = []; // 品牌
  imageList: any[] = []; // 图片
  previewImg = ''; // 图片预览
  imgIsModify = false; // 图片是否被更改，判断是否要上传
  // 文件类型校验
  beforeUpload = (file: NzUploadFile): boolean => {
    const acceptFileType = [
      'bmp', 'jpg', 'png', 'tif', 'gif', 'pcx', 'tga', 'exif',
      'fpx', 'svg', 'psd', 'cdr', 'pcd', 'dxf', 'ufo', 'eps',
    ];
    const fileName: any = file.name.split('.').pop();
    if (!acceptFileType.includes(fileName)) {
      this.nzMessageService.warning('暂不支持该文件类型');
      return false;
    }
    this.getBase64(file, img => {
      this.previewImg = img;
    });
    this.imgIsModify = true;
    this.imageList = [file];
    return false;
  };

  constructor(
    private nzMessageService: NzMessageService,
    public permission: PermissionService,
    private priceService: AisiPriceService,
  ) {
  }

  ngOnInit(): void {
    this.queryTableList();
    this.getBrandAndSeries();
  }

  // 获取品牌
  getBrandAndSeries() {
    this.priceService.httpGet(this.priceService.serviceUrl.brandAndSeries, {}).then((res: any) => {
      if (res.code === 0) {
        this.brandOptions = res.data.bannerNameList;
      }
    });
  }

  getBrandName(id: any) {
    const list = this.brandOptions.filter(item => item.id === id);
    if (list.length) {
      return list[0].name;
    }
    return '';
  }

  // file转base64
  private getBase64(img: any, callback: (img: string) => void): void {
    const reader = new FileReader();
    // @ts-ignore
    reader.addEventListener('load', () => callback(reader.result.toString()));
    reader.readAsDataURL(img);
  }

  // 页码改变
  onPageIndexChange(index: number) {
    this.query.page = index;
  }

  // 分页大小改变
  onPageSizeChange(size: number) {
    this.query.page = 1;
    this.query.pageSize = size;
  }

  // 查询列表
  queryTableList() {
    this.priceService.httpGet(this.priceService.serviceUrl.quoteList, {}).then((res: any) => {
      if (res.code === 0) {
        this.listOfData.records = res.data;
      }
    });
  }

  // 添加报价单模态框显示
  addQuoteModalShow(data?: any) {
    if (data) {
      this.addQuoteModal.id = data.id;
      this.addQuoteModal.nameExport = data.title;
      this.addQuoteModal.nameShow = data.minTitle;
      this.addQuoteModal.brandId = data.bannerId;
      this.addQuoteModal.type = data.priceType;
      this.addQuoteModal.sort = data.sort;
      this.addQuoteModal.status = data.status;
      this.addQuoteModal.imgUrl = data.url;
      this.previewImg = data.url;
    }
    this.addQuoteModal.visible = true;
  }

  // 添加报价单模态框隐藏
  addQuoteModalHide() {
    this.addQuoteModal = {
      visible: false,
      loading: false,
      id: '',
      nameExport: '',
      nameShow: '',
      brandId: '',
      brandName: '',
      type: '',
      sort: '',
      imgUrl: '',
      status: 1,
    };
    this.imageList = []; // 图片
    this.previewImg = ''; // 图片预览
    this.imgIsModify = false; // 图片是否被更改，判断是否要上传
  }

  // 保存
  async addQuoteModalSave() {
    if (!this.addQuoteModal.nameExport) {
      this.nzMessageService.warning('请输入长标题');
      return;
    }
    if (!this.addQuoteModal.nameShow) {
      this.nzMessageService.warning('请输入短标题');
      return;
    }
    if (!this.addQuoteModal.brandId) {
      this.nzMessageService.warning('请选择品牌');
      return;
    }
    if (!(this.addQuoteModal.type + '')) {
      this.nzMessageService.warning('请选择报价类型');
      return;
    }
    if (!this.addQuoteModal.sort) {
      this.nzMessageService.warning('请输入排序');
      return;
    }
    if (this.imgIsModify) {
      this.addQuoteModal.loading = true;
      this.addQuoteModal.imgUrl = await this.priceService.fileUpload(this.imageList[0]) as string;
      this.addQuoteModal.loading = false;
    }
    this.addQuoteModal.loading = true;
    this.priceService.httpPost(this.priceService.serviceUrl.addAndEditQuote, {
      title: this.addQuoteModal.nameExport, // 标题
      id: this.addQuoteModal.id, // 更新才有 主键id
      minTitle: this.addQuoteModal.nameShow, // 小标题
      bannerId: this.addQuoteModal.brandId, // 品牌id
      bannerName: this.addQuoteModal.brandName, // 品牌名称
      priceType: this.addQuoteModal.type, // 0-回收价  1-销售价
      status: this.addQuoteModal.status, // 状态  0-禁用  1-启用 2-删除
      sort: this.addQuoteModal.sort, // 排序
      url: this.addQuoteModal.imgUrl, // 图片
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.queryTableList();
        this.addQuoteModalHide();
      } else {
        this.addQuoteModal.loading = false;
      }
    }).finally(() => {
      this.addQuoteModal.loading = false;
    });
  }

  refreshQuote(id: any) {
    this.priceService.httpGet(this.priceService.serviceUrl.refreshQuote, {id}).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('更新成功');
      } else {
        this.nzMessageService.error(res.message);
      }
    });
  }

  deleteQuote(id: any) {
    this.priceService.httpPost(this.priceService.serviceUrl.addAndEditQuote, {
      id, // 更新才有 主键id
      status: 2, // 状态  0-禁用  1-启用 2-删除
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('删除成功');
        this.queryTableList();
      }
    });
  }
}
